/*
 * @Date 2020-03-23
 * @Author gt333
 * @description 微信webviewjsdk
 * ====
 * @update_date
 * @update_author
 * @update_desc
 * ====
 */

const miniHomePage = "/pages/invoice/invList/invList";

/**
 * 获取小程序环境
 * @param {Function} callback
 */
export const getEnv = callback => {
  window.NUOWX.miniProgram.getEnv(callback);
};

/**
 * 微信jsdk扫一扫功能
 * @param {*} param0
 */
export const scanQRCode = ({
  needResult = 0,
  scanType = "qr",
  success = () => undefined,
  fail = () => undefined
}) => {
  window.NUOWX.scanQRCode({
    needResult, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
    scanType, // 可以指定扫二维码还是一维码，默认二者都有
    success,
    fail: err => {
      console.log(err);
      fail(err);
    }
  });
};

/**
 * 选择图片
 * @param {*} param0
 */
export const chooseImage = ({
  count = 1, // 默认1
  sizeType = ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
  sourceType = ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
  success = () => undefined,
  fail = () => undefined
}) => {
  window.NUOWX.chooseImage({
    count,
    sizeType,
    sourceType,
    success: res => {
      var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
    },
    fail: err => {
      console.log(err);
      fail(err);
    }
  });
};

/**
 * webview 跳转小程序
 * @param {} param0
 */
export const navigateToMiniProgram = ({
  appId = "",
  path = "",
  extraData = null,
  success = () => undefined,
  fail = () => undefined,
  complete = () => undefined
}) => {
  if (appId && path) {
    window.NUOWX.miniProgram.postMessage({
      data: {
        eventName: "navigateToMiniProgram",
        data:{
          path,
          appId,
        },
        extraData
      }
    });
    // 微信小程序跳转其它小程序需要，跳转到首页再打开小程序
    window.NUOWX.miniProgram.switchTab({
      url: miniHomePage,
      success,
      fail,
      complete
    });
  } else {
    fail({ err: "请输入跳转地址或者APPID" });
    console.warn("请输入跳转地址或者APPID");
  }
};

/**
 *
 * 此API只为兼容 支付宝小程序中跳转到支付宝官方业务或运营活动页面。 my.ap.navigateToAlipayPage.
 * web-view 跳转小程序请使用 navigateToMiniProgram
 * @param {*} param0
 */
export const navigateToAlipayPage = ({
  appCode = null,
  appParams = null,
  path = "",
  success = () => undefined,
  fail = () => undefined,
  complete = () => undefined
}) => {
  if (appParams && appCode) {
    window.NUOWX.miniProgram.postMessage({
      data: {
        eventName: "navigateToAlipayPage",
        path,
        appId: appCode,
        extraData: appParams
      }
    });
    // 微信小程序跳转其它小程序需要，跳转到首页再打开小程序
    window.NUOWX.miniProgram.switchTab({
      url: miniHomePage,
      success,
      fail,
      complete
    });
  } else {
    fail({ err: "请输入跳转地址或者APPID" });
  }
};

/**
 * webview 返回
 */
export const navigateBack = () => {
  window.NUOWX.miniProgram.navigateBack({
    fail(err) {
      console.log("err", err);
    },
    complete(err) {
      // 微信小程序 webview 跳转成功此方法不会执行，失败才会执行

      // 容错，防止公众号直接跳入webview调用返回出错
      window.NUOWX.miniProgram.switchTab({
        url: miniHomePage
      });
    }
  });
};

/**
 * webview tabbar 跳转
 * @param {*} param0
 */
export const switchTab = ({ url = miniHomePage, success, fail, complete }) => {
  window.NUOWX.miniProgram.switchTab({
    url,
    fail,
    success,
    complete
  });
};

/**
 * webview 跳转页面
 * @param {*} param0
 */
export const navigateTo = ({ url, success, fail, complete }) => {
  window.NUOWX.miniProgram.navigateTo({
    url,
    fail,
    success,
    complete
  });
};

/**
 * webview 重定向
 * @param {Object} param
 */
export const redirectTo = ({ url, success, fail, complete }) => {
  window.NUOWX.miniProgram.redirectTo({
    url,
    fail,
    success,
    complete
  });
};

/**
 * webview 关闭当前所有页面，跳转到应用内的某个指定页面。
 * @param {Object} param
 */
export const reLaunch = ({ url, success, fail, complete }) => {
  window.NUOWX.miniProgram.reLaunch({
    url,
    success,
    fail,
    complete
  });
};

/**
 * 微信支付
 * @param {Object} param0
 */
export const tradePay = ({
  payStr = "",
  tradeNO = "",
  orderStr = "",
  callbackUrl = "", // 支付回调跳转地址
  success = () => undefined,
  fail = () => undefined
}) => {
  if (orderStr) {
    navigateTo({
      url: `/pages/pay/pay?payParams=${window.encodeURIComponent(
        orderStr
      )}&callbackUrl=${window.encodeURIComponent(
        callbackUrl
      )}&tradeNO=${tradeNO}`
    });
  } else if (payStr) {
    navigateTo({
      url: `/pages/pay/pay?payParams=${window.encodeURIComponent(
        payStr
      )}&callbackUrl=${window.encodeURIComponent(
        callbackUrl
      )}&tradeNO=${tradeNO}`
    });
  } else {
    fail({ err: "支付参数不可为空, 微信小程序需要orderStr或者payStr" });
  }
};

/**
 * webview 定位
 * @param {Object} param
 */
export const getLocation = ({
  type = "wgs84", // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
  success = () => undefined,
  fail = () => undefined
}) => {
  window.NUOWX.getLocation({
    type,
    success,
    fail
  });
};
